/**
 * <p>文件下載簡單類</p>
 * <p>
 * <p>
 * 使用舉例:
 * var simple:Sprite = new SimpleLoader("http://dev.fog.addcn.com/test.swf", { id:'test', x:100,y:100 } );
 * addChild(simple);	
 * 
 * }
 */
package com.addcn.common.event{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import com.addcn.demo.inter.Iloader;
	import com.addcn.demo.data.Global;
	import com.addcn.demo.utils.destroyMemory;
	/**
	 * 文件加載簡單類(單類)
	 * @author lmzd
	 */
	public class SimpleLoader extends Sprite implements Iloader
	{		
		/**
		 * 
		 * @param	param1
		 * @param	param2	文件屬性{id:'',x:100,y:100}
		 */
		public function SimpleLoader(param1:String,param2:Object = null) 
		{
			var  _mc_loaded:Object;
			if (param2 != null ) {
				if ("id" in param2) {
					_mc_loaded = getFile(param2.id);
				}else {
					_mc_loaded = getFile(param1);
				}
			}
			if ( _mc_loaded != null ) {
				var _mc:Sprite = _mc_loaded.content;
				_mc.x = ("x" in param2) ? param2.x : 0;
				_mc.y = ("y" in param2) ? param2.y : 0;							
				addChild(_mc);
				//destroyMemory.clear();
			}else {
				//這裡我們可以指定londing動畫
				var _text:TextField = new TextField();
				_text.text = "正在加載哦......"
				addChild(_text);
				loadFile(param1, param2);
			}		
			
		}
		/**
		 * <b>加載文件</b>
		 * <p>
		 * 下載文件,並更改顯示
		 * @param	param1	文件路徑
		 * @param	param2	文件屬性{id:'',x:100,y:100}
		 */
		public function loadFile(param1:String, param2:Object = null):void {
			var _loader = new Loader();
			var onComplete:Function = function(e):void {
				if ("id" in param2) {
					Global.getInstance()._file[param2.id] = _loader;
				}else {
					Global.getInstance()._file[param1] = _loader;
				}	
				var _mc:Sprite = _loader.content;
				_mc.x = ("x" in param2) ? param2.x : 0;
				_mc.y = ("y" in param2) ? param2.y : 0;		
				while (numChildren > 0) {
					removeChildAt(0);
				}
				addChild(_mc);
				//destroyMemory.clear();
			}
			var onProgress:Function = function(evt):void {
				
			}
			var onIOError:Function = function(e:Event):void {
				
			}
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_loader.load(new URLRequest(param1));				
		}
		/**
		 * 暫無提供xml下載
		 * @param	param1
		 * @param	param2
		 */
		public function loadXML(param1:String, param2:Object = null):void {
			
		}
		/**
		 * <b>獲取文件</b>
		 * <p>
		 * 先從字典中檢查文件是否已經下載過,有下載直接返回.
		 * @param	param	下載文件的標識符
		 * @return	Object	
		 */
		public function getFile(param:String):Object {
			var _temp:Object = Global.getInstance()._file[param];
			if (_temp != null) {		
				return _temp;
			}
			return null;
		}
		
	}

}
